package com.hucii.hc_connectivity;


import android.os.Build;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * The handler receives {@link MethodCall}s from the UIThread, gets the related information from
 * a @{@link Connectivity}, and then send the result back to the UIThread through the {@link
 * MethodChannel.Result}.
 */
class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandler {

    private Connectivity connectivity;

    /**
     * Construct the ConnectivityMethodChannelHandler with a {@code connectivity}. The {@code
     * connectivity} must not be null.
     */
    ConnectivityMethodChannelHandler(Connectivity connectivity) {
        assert (connectivity != null);
        this.connectivity = connectivity;
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        doMethodCall(call, result);
    }

    public void doMethodCall(MethodCall call, MethodChannel.Result result) {
        switch (call.method) {
            case "check":
                result.success(connectivity.check());
                break;
            case "wifiName":
                result.success(connectivity.getWifiName());
                break;
            case "wifiBSSID":
                result.success(connectivity.getWifiBSSID());
                break;
            case "wifiIPAddress":
                result.success(connectivity.getWifiIPAddress());
                break;
            case "isEnabled":
                result.success(connectivity.isEnabled());
                break;
            case "setEnabled":
                connectivity.setEnabled(call, result);
                break;
            case "isConnected":
                connectivity.isConnected(result);
                break;
            case "disconnect":
                connectivity.disconnect(result);
                break;
            case "connect":
                connectivity.connect(call, result);
                break;
            case "forceWifiUsage":
                connectivity.forceWifiUsage(call, result);
                break;
            case "registerWifiNetwork":
                connectivity.registerWifiNetwork(call, result);
                break;
            case "removeWifiNetwork":
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
                    connectivity.removeWifiNetwork(call, result);
                else
                    result.error("Error", "removeWifiNetwork not supported for Android SDK " + Build.VERSION.SDK_INT, null);
                break;
            case "openWifiNetwork":
                connectivity.openWifiNetwork(call, result);
                break;

            default:
                result.notImplemented();
                break;
        }

    }

}